package cn.com.api.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.ApiT_member;
import cn.com.api.entity.ApiT_member_entrepreneur;
import cn.com.api.entity.ApiT_order;
import cn.com.api.entity.ApiT_order_detail;
import cn.com.api.entity.ApiTmember;
import cn.com.api.entity.ApiU_advert;
import cn.com.api.entity.ApiU_advert_resource;
import cn.com.api.entity.ApiU_bonus;

import cn.com.api.mapper.ApiT_memberMapper;
import cn.com.api.mapper.ApiT_member_entrepreneurMapper;
import cn.com.api.mapper.ApiT_orderMapper;
import cn.com.api.mapper.ApiT_order_detailMapper;
import cn.com.api.mapper.ApiU_advertMapper;
import cn.com.api.mapper.ApiU_advert_resourceMapper;
import cn.com.api.mapper.ApiU_bonusMapper;
import cn.com.api.mapper.ApiU_bonus_detailMapper;
import cn.com.api.mapper.ApiU_favoriteMapper;
import cn.com.api.mapper.ApiU_user_labelMapper;
import cn.com.api.mapper.ApiU_zanMapper;

import cn.com.api.service.ApiU_advertService;
import cn.com.api.service.impl.base.U_advertServiceImplBase;
import cn.com.util.DateUtil;
import cn.com.util.JsonUtil;
import cn.com.util.PageInfo;
import cn.com.util.Util;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_advertServiceImpl extends U_advertServiceImplBase implements ApiU_advertService {
	@Autowired
	private ApiU_advertMapper apiU_advertMapper;
	@Autowired
	private ApiU_zanMapper apiU_zanMapper;
	@Autowired
	private ApiU_favoriteMapper apiU_favoriteMapper;
	@Autowired
	private ApiU_bonusMapper apiU_bonusMapper;
	@Autowired
	private ApiU_advert_resourceMapper apiU_advert_resourceMapper;
	@Autowired
	private ApiT_member_entrepreneurMapper apiT_member_entrepreneurMapper;
	@Autowired
	private ApiT_orderMapper apiT_orderMapper;
	@Autowired
	private ApiT_order_detailMapper apiT_order_detailMapper;
	@Autowired
	private ApiT_memberMapper apiT_memberMapper;
	@Autowired
	private ApiU_bonus_detailMapper apiU_bonus_detailMapper;
	@Autowired
	private ApiU_user_labelMapper apiU_user_labelMapper;

	@Override
	public List<ApiU_advert_resource> findU_advert_resourceByAdvertID(String advertID, String user_id) {

		ApiU_advert advert = this.apiU_advertMapper.findU_advertById(Integer.valueOf(advertID));
		if (!Util.isNull(advert)) {
			String retString = this.apiU_bonus_detailMapper.checkHasMoneyBonus(advert.getBonus_id());
			String[] rets = retString.split(":");// 领取的有钱红包数:已领总金额
			if (rets.length != 2) {
				return null;
			}

			List<ApiU_advert_resource> resource_list = this.apiU_advert_resourceMapper
					.findU_advert_resourceByAdvertID(advertID);

			Map<String, String> params = new HashMap<String, String>();
			params.put("user_id", user_id);
			params.put("conn_id", advertID);
			params.put("zan_type", "2");

			// 是否点赞
			Integer is_zan = this.apiU_zanMapper.checkZan(params);
			// 是否收藏
			params.put("type", "4");
			Integer is_favorite = this.apiU_favoriteMapper.checkFavorite(params);
			for (ApiU_advert_resource resource : resource_list) {
				resource.setIs_favorite(is_favorite);
				resource.setIs_zan(is_zan);
			}
			if (advert.getPosition_id() == 8)// 嗨红包没有Resource
			{
				List<ApiU_advert_resource> resources = new ArrayList<ApiU_advert_resource>();
				ApiU_advert_resource resource = new ApiU_advert_resource();
				resources.add(resource);
				return resources;
			} else {
				return resource_list;
			}

		} else {
			return null;
		}
	}

	@Override
	public List<ApiU_advert> findU_advertByPostionID(String user_id, Integer position_id, PageInfo page) {
		List<ApiU_advert> uAdvertsList = this.apiU_advertMapper.findU_advertByPostionIDListPage(position_id, page);
		// 处理缩略图
		for (ApiU_advert uAdvert : uAdvertsList) {
			String covers = uAdvert.getCovers();

			if (position_id == 1) {// 首页大广告
				covers = covers + "?imageView2/1/w/750/h/670";
			} else if (position_id == 2) {// 首页小广告
				covers = covers + "?imageView2/1/w/750/h/292";
			} else if (position_id == 4) {// 红包广告首页轮播
				covers = covers + "?imageView2/1/w/750/h/550";
			} else if (position_id == 5) {// 头条首页轮播
				covers = covers + "?imageView2/1/w/750/h/550";
			} else if (position_id == 7) {// 新发现首页轮播
				covers = covers + "?imageView2/1/w/750/h/550";
			} else if (position_id == 9) {// 特卖汇首页轮播
				covers = covers + "?imageView2/1/w/750/h/550";
			}
			// String logo = uAdvert.getLogo();
			uAdvert.setCovers(covers);
			// uAdvert.setLogo(logo);
		}

		// for(ApiU_advert uAdvert : uAdvertsList)
		// {
		// if(!Util.isNull(user_id))
		// {
		// Map<String, String> params = new HashMap<String, String>();
		// params.put("user_id", user_id);
		// params.put("conn_id", uAdvert.getId().toString());
		// params.put("zan_type", "2");
		//
		// //是否点赞
		// Integer is_zan = this.apiU_zanMapper.checkZan(params);
		// uAdvert.setIs_zan(is_zan);
		// //是否收藏
		// params.put("type", "4");
		// Integer is_favorite = this.apiU_favoriteMapper.checkFavorite(params);
		// uAdvert.setIs_favorite(is_favorite);
		// }
		//
		// List<ApiU_advert_resource> uAdvertResources =
		// this.apiU_advert_resourceMapper.findU_advert_resourceByAdvertID(uAdvert.getId().toString());
		// uAdvert.setAdvert_resources(uAdvertResources);
		// }

		return uAdvertsList;
	}

	@Override
	public List<ApiU_advert> findBonus_AdvertByPostionIDListPage(Map<String, Object> param, PageInfo page) {
		List<ApiU_advert> uAdvertsList = new ArrayList<ApiU_advert>();
		List<ApiU_advert> uMyAdvertsList = new ArrayList<ApiU_advert>();

		if (!Util.isNull(param.get("interesting"))) {
			// 兴趣爱好是否设置
			Integer intCount = 0;
			if (!Util.isNull(param.get("user_id"))) {
				intCount = this.apiU_user_labelMapper.checkUserLabel(param.get("user_id").toString(), 1);
			}

			if (intCount > 0) // 兴趣爱好已设置
			{
				uAdvertsList = this.apiU_advertMapper.findBonus_AdvertByMapListPage(param, page);
				uMyAdvertsList = this.apiU_advertMapper.findMyBonus_AdvertByMapListPage(param, page);
				for (ApiU_advert ad : uMyAdvertsList) {
					uAdvertsList.add(ad);
				}

			} else { // 没有兴趣爱好，系统显示默认的
				uAdvertsList = this.apiU_advertMapper.findInterestBonus_AdvertByMapListPage(param, page);
			}
		} else { // 全品红包
			uAdvertsList = this.apiU_advertMapper.findBonus_AdvertByMapListPage(param, page);
			uMyAdvertsList = this.apiU_advertMapper.findMyBonus_AdvertByMapListPage(param, page);
			for (ApiU_advert ad : uMyAdvertsList) {
				uAdvertsList.add(ad);
			}
		}

		for (ApiU_advert uAdvert : uAdvertsList) {
			// if (param.containsKey("user_id")) {
			if (!Util.isNull(param.get("user_id"))) {
				String user_id = param.get("user_id").toString();
				Map<String, String> params = new HashMap<String, String>();
				params.put("user_id", user_id);
				params.put("conn_id", uAdvert.getId().toString());
				params.put("zan_type", "2");

				// 最大红包额getMaxBonus
				ApiU_bonus u_bonus = this.apiU_bonusMapper.findU_bonusByID(Integer.valueOf(uAdvert.getBonus_id()));
				if (!Util.isNull(u_bonus)) {
					// Float max_money =
					// this.apiU_bonus_detailMapper.getMaxBonus(uAdvert.getBonus_id());
					BigDecimal max_money = u_bonus.getMax_money();
					uAdvert.setMax_money(max_money);
					// 是否点赞
					Integer is_zan = this.apiU_zanMapper.checkZan(params);
					uAdvert.setIs_zan(is_zan);
					// 是否收藏
					params.put("type", "4");
					Integer is_favorite = this.apiU_favoriteMapper.checkFavorite(params);
					uAdvert.setIs_favorite(is_favorite);
				}
			}
			// 缩略图--图片
			if (uAdvert.getResource_type() == 0)// 图片
			{
				String covers = uAdvert.getCovers() + "?imageView2/1/w/750/h/330";
				uAdvert.setCovers(covers);
			}
			// else if (uAdvert.getResource_type() == 1) {
			// String covers = uAdvert.getCovers()
			// +"?vframe/png/offset/3/w/750/h/330";
			// uAdvert.setCovers(covers);
			// }
			List<ApiU_advert_resource> uAdvertResources = this.apiU_advert_resourceMapper
					.findU_advert_resourceByAdvertID(uAdvert.getId().toString());
			if (!Util.isNull(uAdvertResources) && uAdvertResources.size() > 0) {
				// 缩略图--视频
				if (uAdvert.getResource_type() == 1) { // 视频
					String vedio = uAdvertResources.get(0).getResource_url() + "?vframe/png/offset/3/w/750/h/330";
					uAdvert.setCovers(vedio);
				} else if (uAdvert.getResource_type() == 0) {
					for (ApiU_advert_resource resource : uAdvertResources) {
						String resource_url = resource.getResource_url() + "?imageView2/1/w/750/h/1334";
						resource.setResource_url(resource_url);
					}
				}

				uAdvert.setAdvert_resources(uAdvertResources);
			}
		}

		return uAdvertsList;
	}

	@Override
	public void addClickTimes(Integer advert_id) {
		ApiU_advert advert = this.apiU_advertMapper.findU_advertById(advert_id);
		advert.setShow_times(advert.getShow_times() + 1);
		this.apiU_advertMapper.updateU_advert(advert);
	}

	@Override
	public List<ApiU_advert> findBonusU_advertByWords(String keyword, String user_id, PageInfo page) {
		List<ApiU_advert> uAdvertsList = this.apiU_advertMapper.findBonusU_advertByWordsListPage(keyword, user_id,
				page);
		for (ApiU_advert uAdvert : uAdvertsList) {
			if (!Util.isNull(user_id)) {
				Map<String, String> params = new HashMap<String, String>();
				params.put("user_id", user_id);
				params.put("conn_id", uAdvert.getId().toString());
				params.put("zan_type", "2");
				// 点赞数

				// 最大红包额getMaxBonus
				ApiU_bonus u_bonus = this.apiU_bonusMapper.findU_bonusByID(Integer.valueOf(uAdvert.getBonus_id()));
				// Float max_money =
				// this.apiU_bonus_detailMapper.getMaxBonus(uAdvert.getBonus_id());
				BigDecimal max_money = u_bonus.getMax_money();
				uAdvert.setMax_money(max_money);
				// 是否点赞
				Integer is_zan = this.apiU_zanMapper.checkZan(params);
				uAdvert.setIs_zan(is_zan);
				// 是否收藏
				params.put("type", "4");
				Integer is_favorite = this.apiU_favoriteMapper.checkFavorite(params);
				uAdvert.setIs_favorite(is_favorite);
			}

			// 缩略图--图片
			if (uAdvert.getResource_type() == 0)// 图片
			{
				String covers = uAdvert.getCovers() + "?imageView2/1/w/750/h/330";
				uAdvert.setCovers(covers);
			}
			// else if (uAdvert.getResource_type() == 1) {
			// String covers = uAdvert.getCovers()
			// +"?vframe/png/offset/3/w/750/h/330";
			// uAdvert.setCovers(covers);
			// }

			List<ApiU_advert_resource> uAdvertResources = this.apiU_advert_resourceMapper
					.findU_advert_resourceByAdvertID(uAdvert.getId().toString());
			if (!Util.isNull(uAdvertResources) && uAdvertResources.size() > 0) {
				// 缩略图--视频
				if (uAdvert.getResource_type() == 1) { // 视频
					String vedio = uAdvertResources.get(0).getResource_url() + "?vframe/png/offset/3/w/750/h/330";
					uAdvert.setCovers(vedio);
				} else if (uAdvert.getResource_type() == 0) {
					for (ApiU_advert_resource resource : uAdvertResources) {
						String resource_url = resource.getResource_url() + "?imageView2/1/w/750/h/1334";
						resource.setResource_url(resource_url);
					}
				}
				uAdvert.setAdvert_resources(uAdvertResources);
			}
		}

		return uAdvertsList;
	}

	@Override
	public List<ApiU_advert> findCallBonusU_advert(String keyword, String user_id, Integer type, PageInfo page) {
		List<ApiU_advert> uAdvertsList = this.apiU_advertMapper.findCallBonusU_advertListPage(keyword, user_id, type,
				page);
		for (ApiU_advert uAdvert : uAdvertsList) {
			if (!Util.isNull(user_id)) {
				Map<String, String> params = new HashMap<String, String>();
				params.put("user_id", user_id);
				params.put("conn_id", uAdvert.getId().toString());
				params.put("zan_type", "2");
				// 点赞数

				// 最大红包额getMaxBonus
				ApiU_bonus u_bonus = this.apiU_bonusMapper.findU_bonusByID(Integer.valueOf(uAdvert.getBonus_id()));
				BigDecimal max_money = u_bonus.getMax_money();
				uAdvert.setMax_money(max_money);
				// 是否点赞
				Integer is_zan = this.apiU_zanMapper.checkZan(params);
				uAdvert.setIs_zan(is_zan);
				// 是否收藏
				params.put("type", "4");
				Integer is_favorite = this.apiU_favoriteMapper.checkFavorite(params);
				uAdvert.setIs_favorite(is_favorite);
			}

			// 缩略图--图片
			if (uAdvert.getResource_type() == 0)// 图片
			{
				String covers = uAdvert.getCovers() + "?imageView2/1/w/750/h/330";
				uAdvert.setCovers(covers);
			}
			// else if (uAdvert.getResource_type() == 1) {
			// String covers = uAdvert.getCovers()
			// +"?vframe/png/offset/3/w/750/h/330";
			// uAdvert.setCovers(covers);
			// }

			List<ApiU_advert_resource> uAdvertResources = this.apiU_advert_resourceMapper
					.findU_advert_resourceByAdvertID(uAdvert.getId().toString());
			if (!Util.isNull(uAdvertResources) && uAdvertResources.size() > 0) {

				// 缩略图--视频
				if (uAdvert.getResource_type() == 1) { // 视频
					String vedio = uAdvertResources.get(0).getResource_url() + "?vframe/png/offset/3/w/750/h/330";
					uAdvert.setCovers(vedio);
				} else if (uAdvert.getResource_type() == 0) {
					for (ApiU_advert_resource resource : uAdvertResources) {
						String resource_url = resource.getResource_url() + "?imageView2/1/w/750/h/1334";
						resource.setResource_url(resource_url);
					}
				}

				uAdvert.setAdvert_resources(uAdvertResources);
			}
		}

		return uAdvertsList;
	}

	@Override
	public int U_advertInsertion(ApiU_advert u_advert) {
		//
		return this.apiU_advertMapper.U_advertInsertion(u_advert);
	}

	// 根据收藏类型获取收藏列表
	public List<ApiU_advert> findAdvertListByFavoriteType(String user_id, Integer type, PageInfo page) {
		return this.apiU_advertMapper.findAdvertListByFavoriteTypeListPage(user_id, type, page);
	}

	@Override
	public List<Map<String, String>> findU_adverts_infos(Map<String, Object> map) {
		return this.apiU_advertMapper.findU_adverts_infos(map);
	}

	// 创建现场广告红包
	@Override
	public Integer createCallBonus_Advert(Map<String, String> params) {

		String resources[] = params.get("resources").split(":");
		Map<String, String> map = new HashMap<String, String>();
		PageInfo page = new PageInfo();
		page.setCurrentPage(1);
		page.setShowCount(1);
		map.put("user_id", params.get("user_id"));
		map.put("status", "1");
		List<ApiT_member_entrepreneur> entrep_members = this.apiT_member_entrepreneurMapper
				.findT_member_entrepreneursByMapListPage(map, page);
		if (entrep_members.size() == 0) {
			return -1;
		} else {
			ApiU_advert advert = new ApiU_advert();
			advert.setAdv_name(params.get("keyword"));
			advert.setAdv_desc(params.get("keyword"));
			advert.setPosition_id(8);
			advert.setAdd_time(Util.CurrentTime());
			advert.setEnt_id(entrep_members.get(0).getId().toString());
			advert.setAdd_uid(params.get("user_id"));
			advert.setResource_type(0);
			advert.setLogo(params.get("logo"));
			advert.setExt5(params.get("blessword"));
			// advert.setLongitude(params.get("longitude"));
			// advert.setLatitude(params.get("latitude"));
			// advert.setDistance_range(Long.valueOf(params.get("range")));
			advert.setStatus(0);
			advert.setCovers(params.get("cover"));
			this.apiU_advertMapper.U_advertInsertion(advert);
			int sort = 0;
			for (String res : resources) {
				ApiU_advert_resource advert_resource = new ApiU_advert_resource();
				advert_resource.setAdvert_id(advert.getId().toString());
				advert_resource.setResource_url(res);
				advert_resource.setSort(sort++);
				this.apiU_advert_resourceMapper.insertU_advert_resource(advert_resource);
			}
			return 1;
		}
	}

	// 创建现场广告红包(全部)
	@Transactional
	@Override
	public String createCallBonus_Advert_Full(Map<String, String> params) {

		String dingDanNo = "";

		ApiU_bonus bonus = new ApiU_bonus();
		bonus.setUser_id(params.get("user_id"));
		bonus.setTitle(params.get("keyword"));

		Integer bonus_count = Integer.valueOf(params.get("count"));
		BigDecimal bonus_summy = new BigDecimal(params.get("summy"));
		Integer new_bonus_summy = bonus_summy.multiply(new BigDecimal("100")).intValue();
		bonus.setCount(bonus_count);
		bonus.setSummy(bonus_summy);
		// 最小值&最大值
		// if (new_bonus_summy == bonus_count) {
		// bonus.setMax_money(new BigDecimal("0.01"));
		// bonus.setMin_money(new BigDecimal("0.01"));
		// }else {
		//
		// }
		if (bonus_count == 1) {
			bonus.setMax_money(bonus_summy);
			bonus.setMin_money(bonus_summy);
		} else {
			if (new_bonus_summy == bonus_count) {
				bonus.setMax_money(new BigDecimal("0.01"));
				bonus.setMin_money(new BigDecimal("0.01"));
			} else if (new_bonus_summy < bonus_count) {
				// map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
				// map.put(Utils.MESSAGE, "红包个数和金额不匹配！");
				// return map;
				return "";
			} else {
				BigDecimal ave = bonus_summy.divide(new BigDecimal(bonus_count.toString()), 2, RoundingMode.HALF_UP);
				bonus.setMax_money(ave.multiply(new BigDecimal("4")));
				bonus.setMin_money(new BigDecimal("0.01"));
			}
		}

		bonus.setAdd_time(Util.CurrentTime());
		bonus.setIs_expire(0);
		bonus.setType(1);
		bonus.setCount_charge(Integer.valueOf(params.get("count")));
		bonus.setCount_free(0);
		bonus.setLongitude(params.get("longitude"));
		bonus.setLatitude(params.get("latitude"));
		bonus.setBless_word(params.get("blessword"));
		int ret = this.apiU_bonusMapper.U_bonusInsertion(bonus);
		if (ret > 0) {
			ApiU_advert advert = new ApiU_advert();
			advert.setAdv_name(params.get("keyword"));
			advert.setAdv_desc(params.get("keyword"));
			advert.setInterest_key(params.get("keyword"));
			advert.setPosition_id(8);
			advert.setAdd_time(Util.CurrentTime());
			advert.setTotal_amount(new BigDecimal(params.get("summy")));
			advert.setAdd_uid(params.get("user_id"));
			advert.setResource_type(0);
			advert.setStatus(1);
			advert.setCovers(params.get("cover"));
			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
			String date_time = sdf.format(new Date(d.getTime() + 24 * 60 * 60 * 1000));
			advert.setStart_time(Util.CurrentTime());
			advert.setEnd_time(date_time);
			advert.setBonus_id(bonus.getId().toString());
			advert.setLongitude(params.get("longitude"));
			advert.setLatitude(params.get("latitude"));
			advert.setDistance_range(Long.valueOf(params.get("range")));
			ret = this.apiU_advertMapper.U_advertInsertion(advert);
			if (ret > 0) {
				ApiT_order t_order = new ApiT_order();
				t_order.setTitles("");
				t_order.setShop_id("");
				t_order.setUser_id(params.get("user_id"));
				t_order.setPeisong("");// 1 物流 2 自提
				ApiT_member apiTmember = this.apiT_memberMapper
						.findT_memberById(Integer.valueOf(params.get("user_id")));
				if (apiTmember != null) {
					t_order.setName(apiTmember.getC_cname());// 根据userId获取
					t_order.setTel(apiTmember.getC_mob());// 根据userId获取
					t_order.setMobile("");// 根据userId获取
					t_order.setEmail(apiTmember.getC_mail());// 根据userId获取
					t_order.setZip(apiTmember.getC_zip());// 根据userId获取
				}
				t_order.setAddress("");
				t_order.setDtime("");// 送达时间 //待定
				t_order.setBz("");

				t_order.setMoney(new BigDecimal(params.get("summy")));
				t_order.setAppuser("");
				t_order.setAppdate(DateUtil.nowTimeToString());
				t_order.setRip("");
				t_order.setEnable(1);// 未完成订单
				t_order.setZi_status(0);// 未支付
				t_order.setDd_status(1);// 商户未确认
				t_order.setOrdertype(5);// 5-广告订单
				dingDanNo = Utils.getDingDanNo("");
				t_order.setOrdernum(dingDanNo);
				t_order.setSerialflagno(dingDanNo);
				t_order.setPaymoney(new BigDecimal(params.get("summy")));
				t_order.setTitles("现场红包订单");
				ret = apiT_orderMapper.T_orderInsertion(t_order);
				ApiT_order_detail apiT_order_detail = new ApiT_order_detail();
				apiT_order_detail.setMoney(new BigDecimal(params.get("summy")));
				apiT_order_detail.setNum(1L);
				apiT_order_detail.setDid(dingDanNo);
				apiT_order_detail.setTarget_id(advert.getId().toString());
				apiT_order_detailMapper.T_order_detailInsertion(apiT_order_detail);
			}
		}

		if (ret > 0) {
			return dingDanNo;
		} else {
			return "";
		}
	}

	// 更新现场广告红包
	@Override
	public Integer updateCallBonus_Advert(Map<String, String> params) {
		String resources[] = params.get("resources").split(":");
		ApiU_advert advert = this.apiU_advertMapper.findU_advertById(Integer.valueOf(params.get("advert_id")));
		if (advert != null) {
			advert.setAdv_name(params.get("keyword"));
			advert.setAdv_desc(params.get("keyword"));
			advert.setPosition_id(8);
			advert.setAdd_time(Util.CurrentTime());
			advert.setAdd_uid(params.get("user_id"));
			advert.setResource_type(0);
			advert.setLogo(params.get("logo"));
			advert.setExt5(params.get("blessword"));
			advert.setStatus(0);
			advert.setCovers(params.get("cover"));
			this.apiU_advertMapper.updateU_advert(advert);
			int sort = 0;
			for (String res : resources) {
				ApiU_advert_resource advert_resource = new ApiU_advert_resource();
				advert_resource.setAdvert_id(advert.getId().toString());
				advert_resource.setResource_url(res);
				advert_resource.setSort(sort++);
				this.apiU_advert_resourceMapper.insertU_advert_resource(advert_resource);
			}
			return 1;
		} else {
			return -1;
		}

	}

	// 现场广告红包继续支付
	@Override
	public ApiU_advert updateCallBonus_Advert_Repay(Map<String, String> params) {
		return this.apiU_advertMapper.findU_advertById(Integer.valueOf(params.get("advert_id")));
	}

	// 取消现场广告红包
	@Override
	public int cancelCallBonus_Advert(Map<String, String> params) {
		int ret = 0;
		ApiU_advert advert = this.apiU_advertMapper.findU_advertById(Integer.valueOf(params.get("advert_id")));
		advert.setStatus(0);
		ret = this.apiU_advertMapper.updateU_advert(advert);
		return ret;
	}

	// 确认现场广告红包,返回生成的订单号
	@Override
	public String comfirmCallBonus_Advert(Map<String, String> params) throws Exception {
		ApiU_advert advert = this.apiU_advertMapper.findU_advertById(Integer.valueOf(params.get("advert_id")));

		ApiU_bonus bonus = new ApiU_bonus();
		bonus.setUser_id(params.get("user_id"));
		bonus.setCount(Integer.valueOf(params.get("count")));
		bonus.setSummy(new BigDecimal(params.get("summy")));
		bonus.setTitle(params.get("title"));
		bonus.setAdd_time(Util.CurrentTime());
		bonus.setIs_expire(0);
		bonus.setType(1);
		bonus.setCount_charge(Integer.valueOf(params.get("count")));
		bonus.setCount_free(0);
		bonus.setLongitude(params.get("longitude"));
		bonus.setLatitude(params.get("latitude"));
		bonus.setBless_word(advert.getExt5());// 取临时存储的数据
		this.apiU_bonusMapper.U_bonusInsertion(bonus);

		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String date_time = sdf.format(new Date(d.getTime() + 24 * 60 * 60 * 1000));
		advert.setStart_time(Util.CurrentTime());
		advert.setEnd_time(date_time);
		advert.setBonus_id(bonus.getId().toString());
		advert.setLongitude(params.get("longitude"));
		advert.setLatitude(params.get("latitude"));
		advert.setDistance_range(Long.valueOf(params.get("range")));
		this.apiU_advertMapper.updateU_advert(advert);

		// 生成支付订单
		ApiT_order order = new ApiT_order();
		// 生成的订单号
		String rid = Utils.getDingDanNo("");
		order.setRid(Integer.valueOf(rid));
		order.setAppdate(DateUtil.nowTimeToString());
		order.setEnable(1);
		order.setMoney(new BigDecimal(params.get("summy")));
		order.setOrdertype(5);
		order.setPaymoney(new BigDecimal(params.get("summy")));
		order.setUser_id(params.get("user_id"));
		order.setTitles("现场红包订单");
		order.setZi_status(0);
		apiT_orderMapper.T_orderInsertion(order);
		ApiT_order_detail apiT_order_detail = new ApiT_order_detail();
		apiT_order_detail.setMoney(new BigDecimal(params.get("summy")));
		apiT_order_detail.setNum(1L);
		apiT_order_detail.setDid(rid);
		apiT_order_detail.setTarget_id(params.get("advert_id"));
		apiT_order_detailMapper.T_order_detailInsertion(apiT_order_detail);
		return rid;

	}

	// 用户发的现场红包广告列表
	@Override
	public List<ApiU_advert> findCallBonusU_advertByUserID(String user_id, PageInfo page) {

		return this.apiU_advertMapper.findCallBonusU_advertByUserIDListPage(user_id, page);
	}

	public ApiU_advert findAdvertsInfoByMap(Map<String, Object> map) {
		return this.apiU_advertMapper.findAdvertsInfoByMap(map);
	}

	public List<ApiU_advert> findAdvertInfos(Map<String, Object> map) {
		return this.apiU_advertMapper.findAdvertInfos(map);
	}

	// 创建普通红包
	@Override
	public Map<String, Object> createNomalBonus(Map<String, String> params) {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> result = new HashMap<String, Object>();
		int ret = 0;
		// 生成的订单号
		String rid = "";

		ApiU_bonus bonus = new ApiU_bonus();
		bonus.setUser_id(params.get("user_id"));
		Integer bonus_count = Integer.valueOf(params.get("count"));
		BigDecimal bonus_summy = new BigDecimal(params.get("summy"));
		Integer new_bonus_summy = bonus_summy.multiply(new BigDecimal("100")).intValue();

		bonus.setCount(bonus_count);
		bonus.setSummy(bonus_summy);
		// 最小值&最大值
		if (bonus_count == 1) {
			bonus.setMax_money(bonus_summy);
			bonus.setMin_money(bonus_summy);
		} else {
			if (new_bonus_summy == bonus_count) {
				bonus.setMax_money(new BigDecimal("0.01"));
				bonus.setMin_money(new BigDecimal("0.01"));
			} else if (new_bonus_summy < bonus_count) {
				map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
				map.put(Utils.MESSAGE, "红包个数和金额不匹配！");
				return map;
			} else {
				BigDecimal ave = bonus_summy.divide(new BigDecimal(bonus_count.toString()), 2, RoundingMode.HALF_UP);
				bonus.setMax_money(ave.multiply(new BigDecimal("4")));
				bonus.setMin_money(new BigDecimal("0.01"));
			}
		}

		bonus.setAdd_time(Util.CurrentTime());
		bonus.setIs_expire(0);
		bonus.setType(0);
		bonus.setCount_charge(Integer.valueOf(params.get("count")));
		bonus.setCount_free(0);
		bonus.setBless_word(params.get("blessword"));
		ret = this.apiU_bonusMapper.U_bonusInsertion(bonus);

		if (ret > 0) {
			ApiT_order t_order = new ApiT_order();
			t_order.setTitles("");
			t_order.setShop_id("");
			t_order.setUser_id(params.get("user_id"));
			t_order.setPeisong("");// 1 物流 2 自提
			ApiT_member apiTmember = this.apiT_memberMapper.findT_memberById(Integer.valueOf(params.get("user_id")));
			if (apiTmember != null) {
				t_order.setName(apiTmember.getC_cname());// 根据userId获取
				t_order.setTel(apiTmember.getC_mob());// 根据userId获取
				t_order.setMobile("");// 根据userId获取
				t_order.setEmail(apiTmember.getC_mail());// 根据userId获取
				t_order.setZip(apiTmember.getC_zip());// 根据userId获取
			}
			t_order.setAddress("");
			t_order.setDtime("");// 送达时间 //待定
			t_order.setBz("普通红包");

			t_order.setMoney(new BigDecimal(params.get("summy")));
			t_order.setAppuser("");
			t_order.setAppdate(DateUtil.nowTimeToString());
			t_order.setRip("");
			t_order.setEnable(1);// 未完成订单
			t_order.setZi_status(0);// 未支付
			t_order.setDd_status(1);// 商户未确认
			t_order.setOrdertype(6);// 6-普通红包订单
			String dingDanNo = Utils.getDingDanNo("");
			t_order.setOrdernum(dingDanNo);
			t_order.setSerialflagno(dingDanNo);
			t_order.setPaymoney(new BigDecimal(params.get("summy")));
			t_order.setTitles("普通红包订单");
			ret = apiT_orderMapper.T_orderInsertion(t_order);
			ApiT_order_detail apiT_order_detail = new ApiT_order_detail();
			apiT_order_detail.setMoney(new BigDecimal(params.get("summy")));
			apiT_order_detail.setNum(1L);
			apiT_order_detail.setDid(dingDanNo);
			apiT_order_detail.setTarget_id(bonus.getId().toString());
			ret = apiT_order_detailMapper.T_order_detailInsertion(apiT_order_detail);

			result.put("ordernum", dingDanNo);
			result.put("paymoney", new BigDecimal(params.get("summy")));
			result.put("paystatus", 0);
		}
		if (ret > 0) {
			result.put("bonus", bonus);
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "操作成功");
			map.put(Utils.DATAS, result);
		} else {
			map.put(Utils.RESULT, Utils.RESULT_RC_PARAMTER_ERROR);
			map.put(Utils.MESSAGE, "操作失败");
		}
		return map;
	}

	@Transactional
	public Map<String, Object> create_advert_positionadvert(ApiTmember apiTmember, ApiU_advert u_advert)
			throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", apiTmember.getCid());
		ApiT_member_entrepreneur apiT_member_entrepreneur = apiT_member_entrepreneurMapper
				.findMemberEntById(apiTmember.getCid() + "");
		String entrepreneurId = apiT_member_entrepreneur.getId() + "";
		map.clear();
		if (apiTmember.getVerify_entrepreneur() != 1) {
			map.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			map.put(Utils.MESSAGE, "用户不是企业主！");
		} else {
			Integer advertid = u_advert.getId();
			if (advertid == null || advertid == 0) {// 添加
				u_advert.setZan_count(0);
				u_advert.setEnt_id(entrepreneurId);// 企业id
				u_advert.setAdd_uid(apiTmember.getCid().toString());// 发布人id
				u_advert.setAdd_time(DateUtil.nowTimeToString());
				u_advert.setStatus(0);
				u_advert.setIs_pay(0);
				u_advert.setLogo(apiT_member_entrepreneur.getLogo_img());
				String hourinfo = DateUtil.nowTimeToString().split(" ")[1];
				u_advert.setStart_time(u_advert.getStart_time() + " " + hourinfo);
				u_advert.setEnd_time(u_advert.getEnd_time() + " " + hourinfo);
				apiU_advertMapper.U_advertInsertion(u_advert);
				// 生成支付订单
				ApiT_order order = new ApiT_order();
				// 生成的订单号
				String ordernum = Utils.getDingDanNo("");
				order.setAppdate(DateUtil.nowTimeToString());
				order.setEnable(1);
				order.setMoney(u_advert.getTotal_amount());
				order.setOrdertype(5);
				order.setOrdernum(ordernum);
				order.setSerialflagno(ordernum);
				order.setPaymoney(u_advert.getTotal_amount());
				order.setUser_id(apiTmember.getCid().toString());
				order.setTitles("广告位订单");
				order.setZi_status(0);// 带支付
				order.setDd_status(1);// 待支付
				apiT_orderMapper.T_orderInsertion(order);
				ApiT_order_detail apiT_order_detail = new ApiT_order_detail();
				apiT_order_detail.setMoney(u_advert.getTotal_amount());
				apiT_order_detail.setNum(1L);
				apiT_order_detail.setDid(ordernum);
				apiT_order_detail.setTarget_id(u_advert.getId() + "");
				apiT_order_detailMapper.T_order_detailInsertion(apiT_order_detail);
				map.put(Utils.DATAS, order.getRid());
			} else {// 修改
				ApiU_advert advert = apiU_advertMapper.findU_advertById(advertid);
				advert.setResource_type(u_advert.getResource_type());
				advert.setCovers(u_advert.getCovers());
				advert.setSort(u_advert.getSort());
				advert.setTemplate_id(u_advert.getTemplate_id());
				// 如果是驳回状态修改后重新提交审核
				if (advert.getStatus() == 3) {
					advert.setStatus(0);// 修改为待审核状态
					advert.setExt1(null);// 清空驳回原因
				}
				apiU_advertMapper.updateU_advert(advert);
			}
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		}
		return map;
	}

	public List<ApiU_advert> getMyAdvertInfos(Integer cid, PageInfo pageInfo) throws Exception {
		List<ApiU_advert> advertList = null;
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("user_id", cid);
		List<Map<String, Object>> list = apiT_member_entrepreneurMapper.findMember_entrepreneursByMap(map);
		if (list != null && list.size() > 0) {
			Map<String, Object> result = list.get(0);
			String entrepreneurId = result.get("ID").toString();
			map.clear();
			map.put("advertType", "position");
			map.put("ent_id", entrepreneurId);
			advertList = apiU_advertMapper.findAdvertInfos(map);
		}
		return advertList;
	}

	public void deleteAdvertInfo(Integer advertid) throws Exception {
		ApiU_advert advert = apiU_advertMapper.findU_advertById(advertid);
		advert.setIs_delete(1);
		apiU_advertMapper.updateU_advert(advert);
		if (advert.getBonus_id() != null && !"".equals(advert.getBonus_id())) {// 有所属红包，则红包也要删除
			apiU_bonusMapper.deleteBonus(Integer.valueOf(advert.getBonus_id()));
		}
	}

	public List<ApiU_advert> getMyHaiBonusList(String user_id, PageInfo page) {
		return this.apiU_advertMapper.getMyHaiBonusListPage(user_id, page);
	}
}
